import {
  Controller,
  Get,
  Post,
  Param,
  Query,
  UseGuards,
  Request,
  Delete,
} from '@nestjs/common';
import { NotificationService } from './notification.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';

@Controller('api/notification')
@UseGuards(JwtAuthGuard)
export class NotificationController {
  constructor(private readonly notificationService: NotificationService) {}

  @Get('list')
  async findAll(@Query() query, @Request() req) {
    const [notifications, total] = await this.notificationService.findAll(
      req.user.sub,
      query,
    );
    return {
      code: 200,
      message: '获取成功',
      data: {
        list: notifications,
        total,
      },
    };
  }

  @Get('unread-count')
  async getUnreadCount(@Request() req) {
    const count = await this.notificationService.getUnreadCount(req.user.sub);
    return {
      code: 200,
      message: '获取成功',
      data: count,
    };
  }

  @Post(':id/read')
  async markAsRead(@Param('id') id: string, @Request() req) {
    const notification = await this.notificationService.markAsRead(
      +id,
      req.user.sub,
    );
    return {
      code: 200,
      message: '标记成功',
      data: notification,
    };
  }

  @Post('read-all')
  async markAllAsRead(@Request() req) {
    await this.notificationService.markAllAsRead(req.user.sub);
    return {
      code: 200,
      message: '标记成功',
    };
  }

  @Delete(':id')
  async remove(@Param('id') id: string, @Request() req) {
    await this.notificationService.remove(+id, req.user.sub);
    return {
      code: 200,
      message: '删除成功',
    };
  }
}
